package com.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fire.mapper.LocationMapper;
import com.fire.mapper.MonitorMapper;
import com.fire.model.entity.Monitor;
import com.fire.model.enums.DisposeEnum;
import com.fire.service.IMonitorService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ClassName: MonitorServiceImpl
 * Package: com.fire.service.impl
 * Description:
 *
 * @Author: @weixueshi
 * @Create: 2024/4/3 - 23:33
 * @Version: v1.0
 */
@Service
@RequiredArgsConstructor
public class MonitorServiceImpl extends ServiceImpl<MonitorMapper, Monitor> implements IMonitorService {

    private final LocationMapper locationMapper;

    @Override
    public Map<String, Object> queryMonitorData(Integer pageNo, Integer pageSize, String location) {
        Page<Monitor> pageInfo = new Page<>(pageNo, pageSize);
        String locationTrim = "";
        if(!StringUtils.isBlank(location)){
            locationTrim = location.trim();
        }
        QueryWrapper<Monitor> wrapper = new QueryWrapper<>();
        wrapper.like("location", locationTrim);
        wrapper.orderByDesc("create_time");
        Page<Monitor> monitorPage = baseMapper.selectPage(pageInfo, wrapper);
        if(null != monitorPage){
            Map<String,Object> map = new HashMap<>();
            map.put("data", monitorPage.getRecords());
            map.put("total", monitorPage.getTotal());
            return map;
        }
        return null;
    }

    @Override
    public List<String> queryCity() {
        List<String> cities = baseMapper.selectList(null)
                .stream().map(item -> item.getLocation())
                .collect(Collectors.toList());
        return cities;
    }

    @Override
    public void updateDispose(Integer id) {
        Monitor monitor = baseMapper.selectById(id);
        monitor.setIsDispose(DisposeEnum.PROCESSED);
        baseMapper.updateById(monitor);
    }
}
